package com.zoo.diannaopeijian.comm.sql;

import org.springframework.util.StringUtils;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by zx583 on 2018-01-14.
 */
public class GeneratorExampleBuilder {
    public static Object buildEqualExample(Class exampleClazz, Object object, boolean canAllNull) {
        if (exampleClazz == null) {
            throw new NullPointerException("exampleClazz can not be null");
        }

        Object example = null;

        try {
            example = exampleClazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        if (example == null) {
            return null;
        }

        if (object == null) {
            return canAllNull ? example : null;
        }

        Class objectClazz = object.getClass();

        Object criterion = null;
        try {
            criterion = exampleClazz.getMethod("createCriteria").invoke(example);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        Class criterionClazz = criterion.getClass();

        for (Field field : objectClazz.getDeclaredFields()) {
            // 获取get方法
            PropertyDescriptor pd = null;
            try {
                pd = new PropertyDescriptor(field.getName(), objectClazz);
            } catch (IntrospectionException e) {
                e.printStackTrace();
            }

            if (pd == null) {
                continue;
            }

            // 判断值是否为空
            Object paramValue = null;

            try {
                paramValue = pd.getReadMethod().invoke(object);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

            if (paramValue == null) {
                continue;
            }

            if (paramValue instanceof String && StringUtils.isEmpty(paramValue)) {
                continue;
            }

            // 加入判断条件
            if (criterion == null) {
                break;
            }

            try {
                canAllNull = true;
                criterionClazz.getMethod(getEqualToString(field.getName()), paramValue.getClass()).invoke(criterion, paramValue);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }

        return canAllNull ? example : null;
    }

    public static String getEqualToString(String name) {
        StringBuilder sb = new StringBuilder();

        sb.append("and");
        sb.append(Character.toUpperCase(name.charAt(0)));
        sb.append(name.substring(1));
        sb.append("EqualTo");

        return sb.toString();
    }
}
